/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rpt.bd.cenario;

import java.awt.Color;
import java.awt.Component;
import java.math.BigDecimal;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.DateFormatter;
import rpt.lib.CCalendarioComboBox;
import rpt.lib.CCoresComboBox;

/**
 *
 * @author Roberto
 */
/**************************************************************************/
/***                                                                    ***/
/**************************************************************************/
public class CEstruturaCampoRegistroBd {

    protected CEstruturaRegistroBd fEstruturaRegistroBd = null;
    protected CPersistenciaBd fPertinenciaBd = null;
    protected CGlobal fGlobal = null;
    protected CConectorBd fConectorBd = null;
    protected String fTitulo = null;
    protected String fNomeBd = null;
    protected Component fComponenteCampo = null;
    //protected String fValorOriginal = null;
    protected ETipoCampoBd fTipoCampo = ETipoCampoBd.indefinido;
    protected boolean fCampoGradeBd = false;
    protected int fLarguraGrade = 0;
    protected boolean fEditavel = true;
    protected boolean fSelect = true;
    protected boolean fInsert = true;
    protected boolean fUpdate = true;
    protected int fPosicaoColunaRelatorio;
    protected int fLarguraColunaRelatorio;
    protected CEventoEstruturaCampoRegistroBd fEventoEstruturaCampoRegistroBd = null;
    protected int fIndiceTab = 0;

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CEstruturaCampoRegistroBd() {
        super();
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CEstruturaCampoRegistroBd(
            CEstruturaRegistroBd aEstruturaRegistroBd,
            String aTitulo,
            String aNomeBd,
            Component aComponenteCampo,
            String aValorOriginal,
            ETipoCampoBd aTipoCampo,
            boolean aCampoGradeBd,
            boolean aCampoObrigatorio,
            int aLarguraGrade,
            boolean aEditavel,
            boolean aSelect,
            boolean aInsert,
            boolean aUpdate,
            int aPosicaoColunaRelatorio,
            int aLarguraColunaRelatorio) {
        //
        fEstruturaRegistroBd = aEstruturaRegistroBd;
        fGlobal = fEstruturaRegistroBd.getGlobal();
        fPertinenciaBd = fEstruturaRegistroBd.getPertinenciaBd();
        if (fPertinenciaBd != null) {
            fConectorBd = fPertinenciaBd.getConectorBd();
        }
        //
        fTitulo = aTitulo;
        fNomeBd = aNomeBd;
        fComponenteCampo = aComponenteCampo;
        fTipoCampo = aTipoCampo;
        fCampoGradeBd = aCampoGradeBd;
        fLarguraGrade = aLarguraGrade;
        fEditavel = aEditavel;
        fSelect = aSelect;
        fInsert = aInsert;
        fUpdate = aUpdate;
        //
        fPosicaoColunaRelatorio = aPosicaoColunaRelatorio;
        fLarguraColunaRelatorio = aLarguraColunaRelatorio;
        //
        fEventoEstruturaCampoRegistroBd = new CEventoEstruturaCampoRegistroBd();
        //
        if (fComponenteCampo instanceof JTextField) {
            JTextField tf = (JTextField) fComponenteCampo;
            tf.setDisabledTextColor(Color.blue);
        }
        if (fComponenteCampo instanceof JComboBox) {
            JComboBox cb = (JComboBox) fComponenteCampo;
            cb.setBackground(Color.red);
            Component c = cb.getEditor().getEditorComponent();
            if (c instanceof JTextField) {
                JTextField tf = (JTextField) c;
                tf.setDisabledTextColor(Color.blue);
                tf.setBackground(Color.red);
            }
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public final String getObjectStr(Object aValor) {
        if (aValor == null) {
            return null;
        }
        String vValorStr = "";
        if (aValor instanceof String) {
            vValorStr = ((String) aValor);
        } else {
            if (aValor instanceof Integer) {
                vValorStr = ((Integer) aValor).toString();
            } else {
                if (aValor instanceof BigDecimal) {
                    vValorStr = ((BigDecimal) aValor).toString();
                } else {
                    if (aValor instanceof Boolean) {
                        vValorStr = ((Boolean) aValor).toString();
                    } else {
                        if (aValor instanceof Color) {
                            vValorStr = ((Color) aValor).toString();
                        } else {
                            if (aValor instanceof Date) {
                                Date d = (Date) aValor;
                                vValorStr = new SimpleDateFormat("dd/MM/yyyy").format(d); // vValor;d.getDay() + "/" + d.getMonth() + "/" + d.getYear(); xxx
                            } else {
                                if (aValor instanceof Time) {
                                    Time f = (Time) aValor;
                                    vValorStr = new SimpleDateFormat("HH:mm:ss").format(f); // vValor;d.getDay() + "/" + d.getMonth() + "/" + d.getYear(); xxx
                                }
                            }
                        }
                    }
                }
            }
        }
        return vValorStr;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public final int setValorComponente(
            Object aValor) {
        //if (aValor == null) return -1;
        final Component c = getComponente();
        if (c == null) {
            return -1;
        }
        if (c instanceof JTextArea) {
            ((JTextArea) c).setText(getObjectStr(aValor));
            return 1;
        }
        if (c instanceof JFormattedTextField) {
            JFormattedTextField ftf = (JFormattedTextField) c;
            if ((aValor != null) && (aValor instanceof String) && (ftf.getFormatter() instanceof DateFormatter)) {
                try {
                    aValor = new SimpleDateFormat("dd/MM/yyyy").parse((String) aValor);
                } catch (ParseException ex) {
                    aValor = null;
                    Logger.getLogger(CEstruturaCampoRegistroBd.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            ftf.setValue(aValor);
            /*
            try {
                ftf.commitEdit();
            } catch (ParseException ex) {
                Logger.getLogger(CCampoRegistroBd.class.getName()).log(Level.SEVERE, null, ex);
            }
             *
             */
            return 1;
        }
        if (c instanceof JTextField) {
            ((JTextField) c).setText(getObjectStr(aValor));
            return 1;
        }
        if (c instanceof CCalendarioComboBox) {
            final CCalendarioComboBox ccb = (CCalendarioComboBox) c;
            boolean vEditavel = ccb.isEditable();
            ccb.setEditable(true);
            /*
            if (aValor instanceof String) {
                String s = (String) aValor;
                try {
                    aValor = new SimpleDateFormat("dd/MM/yyyy").parse(s);
                } catch (ParseException ex) {
                    Logger.getLogger(CEstruturaCampoRegistroBd.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            ccb.getComponenteTextoFormatado().setValue(aValor);
            try {
                ccb.getComponenteTextoFormatado().commitEdit();
            } catch (ParseException ex) {
                Logger.getLogger(CEstruturaCampoRegistroBd.class.getName()).log(Level.SEVERE, null, ex);
            }
            */
            ccb.setSelectedItem(aValor);
            ccb.setEditable(vEditavel);
            return 1;
        }
        if (c instanceof CCoresComboBox) {
            final CCoresComboBox ccb = (CCoresComboBox) c;
            ccb.setSelectedItem(aValor);
            return 1;
        }
        if (c instanceof JComboBox) {
            final JComboBox cbx = (JComboBox) c;
            final int idx = cbx.getSelectedIndex();
            final Object o = cbx.getItemAt(idx);
            //String s = cbx.get
            return setValorComboBox(aValor);
        }
        if (c instanceof JCheckBox) {
            final JCheckBox cb = (JCheckBox) c;
            if (aValor instanceof Boolean) {
                cb.setSelected((Boolean) aValor);
            } else {
                cb.setSelected(false);
            }
            return 1;
        }
        if (c instanceof JRadioButton) {
            final JRadioButton rb = (JRadioButton) c;
            if (aValor instanceof Boolean) {
                rb.setSelected((Boolean) aValor);
            } else {
                rb.setSelected(false);
            }
            return 1;
        }
        if (c instanceof JSpinner) {
            final JSpinner rb = (JSpinner) c;
            if (aValor instanceof Integer) {
                rb.setValue((Integer) aValor);
            } else {
                rb.setValue(new Integer(1));
            }
            return 1;
        }
        return 0;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CEventoEstruturaCampoRegistroBd getEventoEstruturaCampoRegistroBd() {
        return fEventoEstruturaCampoRegistroBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public CEstruturaRegistroBd getEstruturaRegistroBd() {
        return fEstruturaRegistroBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void setEventoEstruturaCampoRegistroBd(CEventoEstruturaCampoRegistroBd aEvento) {
        fEventoEstruturaCampoRegistroBd = aEvento;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public String getTitulo() {
        return fTitulo;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public String getNomeBd() {
        return fNomeBd;
    }
    /*
    /**************************************************************************/

    /***                                                                    ***/
    /**************************************************************************/
    /*
    public String getValorObjeto(Object aObject) {
    if (aObject == null) return null;
    if (aObject instanceof JTextField) return ((JTextField)aObject).getText();
    return null;
    }
     */
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    /*
    public String getValorComboBoxStr(JComboBox aComboBox) {
    Object o = aComboBox.getModel().getSelectedItem();
    if (o == null)
    return "";
    else {
    //if (o instanceof )
    return o.toString();
    }
    }
     */
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    /*
    public String getValorComponenteStr(Component aComponenteCampo) {
    if (aComponenteCampo == null)
    return null;
    if (aComponenteCampo instanceof JTextField)
    return ((JTextField)aComponenteCampo).getText();
    if (aComponenteCampo instanceof CCalendarioComboBox) {
    CCalendarioComboBox c = (CCalendarioComboBox)aComponenteCampo;
    Object o = ((CCalendarioComboBox)aComponenteCampo).getModel().getSelectedItem();
    if (o == null)
    return "";
    else {
    Color vCor = ((Color)o);
    Integer i = new Integer(vCor.getRGB());
    return i.toString();
    }
    }
    if (aComponenteCampo instanceof CCoresComboBox) {
    Object o = ((CCoresComboBox)aComponenteCampo).getModel().getSelectedItem();
    if (o == null)
    return "";
    else
    return new String(o.toString());
    //          return Calendar.getInstance().toString();
    }
    if (aComponenteCampo instanceof JComboBox) {
    return getValorComboBoxStr((JComboBox)aComponenteCampo);
    }
    return "???";
    }
    
    public String getValorComponenteStr() {
    return getValorComponenteStr(fComponenteCampo);
    }
     */
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    /*
    public String getValorComponenteStr(CCampoRegistroBd aCampoRegistroBd) {
    //String vResultado = null;
    //String x = aCampoRegistroBd;
    String vResultado = getValorComponenteStr();
    if (vResultado != null) return "";
    if (fComponenteCampo instanceof JComboBox)
    vResultado = getValorObjeto(((JComboBox)fComponenteCampo).getModel().getSelectedItem());
    return vResultado;
    }
     */
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public int setValorComboBox(Object aValor) {
        return -1;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    /*
    public int setValorComponente(Object aValor) {
    if (fComponenteCampo == null) return -1;
    if (fComponenteCampo instanceof JTextField) {
    String vValorStr = null;
    if (aValor instanceof String) {
    vValorStr = ((String)aValor);
    setValorCampoRegistroBd(vValorStr,false);
    }
    else {
    if (aValor instanceof Integer) {
    vValorStr = ((Integer)aValor).toString();
    setValorCampoRegistroBd(vValorStr,false);
    }
    else {
    if (aValor instanceof Boolean) {
    vValorStr = ((Boolean)aValor).toString();
    setValorCampoRegistroBd(vValorStr,false);
    }
    else {
    if (aValor instanceof Color) {
    vValorStr = ((Color)aValor).toString();
    setValorCampoRegistroBd(vValorStr,false);
    }
    }
    }
    }
    ((JTextField)fComponenteCampo).setText(vValorStr);
    return 1;
    }
    else
    if (fComponenteCampo instanceof CCalendarioComboBox) {
    CCalendarioComboBox c = (CCalendarioComboBox)fComponenteCampo;
    c.setSelectedItem(aValor);
    return 1;
    }
    if (fComponenteCampo instanceof CCoresComboBox) {
    CCoresComboBox c = (CCoresComboBox)fComponenteCampo;
    c.setSelectedItem(aValor);
    return 1;
    }
    if (fComponenteCampo instanceof JComboBox) {
    return setValorComboBox(aValor);
    }
    if (fComponenteCampo instanceof JCheckBox) {
    JCheckBox cb = (JCheckBox)fComponenteCampo;
    if (aValor instanceof Boolean)
    cb.setSelected((Boolean)aValor);
    else
    cb.setSelected(false);
    return 1;
    }
    return 0;
    }
     */
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    /*
    public int setValorComponente(String aValor) {
    if (fComponenteCampo == null) return -1;
    if (fComponenteCampo instanceof JTextField) {
    ((JTextField)fComponenteCampo).setText(aValor);
    return 1;
    }
    else
    if (fComponenteCampo instanceof CCalendarioComboBox) {
    CCalendarioComboBox c = (CCalendarioComboBox)fComponenteCampo;
    JFormattedTextField t = c.getComponenteTextoFormatado();
    if (aValor == null)
    t.setValue("");
    else
    t.setValue(aValor);
    //t.setValue(aValor);
    //Object vItem = c.getModel().getSelectedItem();
    
    //vResultado = getValorCampo(fComponenteCampo,vItem);
    }
    else
    if (fComponenteCampo instanceof JComboBox) {
    JComboBox cb = (JComboBox)fComponenteCampo;
    Vector vReg = (Vector)cb.getSelectedItem();
    aValor = vReg.get(0).toString();
    //          Object vItem = ((JComboBox)fComponenteCampo).getModel().getSelectedItem();
    //          String s = null;
    //          if (vItem == null) s = "";
    //          else
    //              if (vItem instanceof String) {
    //                  s = (String)vItem;
    //                  s = aValor;
    //              }
    //vResultado = getValorCampo(fComponenteCampo,vItem);
    }
    setValorCampoRegistroBd(aValor,false);
    return 0;
    }
     */
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    /*
    public int setValorComponenteStr(Component aComponente) {
    if (aComponente == null) return -1;
    if (aComponente instanceof JTextField) {
    ((JTextField)aComponente).setText(getValorComponenteStr());
    return 1;
    }
    else
    if (aComponente instanceof JComboBox) {
    Object vItem = ((JComboBox)aComponente).getModel().getSelectedItem();
    //vResultado = getValorCampo(fComponenteCampo,vItem);
    return 1;
    }
    return 0;
    }
     */
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    /*
    public int setValorCampoRegistroBd(Object aValor, boolean aAtualizarComponente) {
    //fValorOriginal = aValor;
    if (aAtualizarComponente) {
    setValorComponente(aValor);
    }
    return 1;
    }
     */
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    /*
    public int setValorCampoRegistroBd(String aValor) {
    return setValorCampoRegistroBd(aValor,true);
    }
     */
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    /*
    public String getValorCampoRegistroBdStr(Component aComponenteCampo) {
    //String vResultado = null;
    //String x = aCampoRegistroBd;
    String vResultado = getValorComponenteStr(aComponenteCampo);
    if ((vResultado == null) && (aComponenteCampo instanceof CCalendarioComboBox))
    //Object o = ((JComboBox)fComponenteCampo).getModel().getSelectedItem();
    vResultado = getValorComponenteStr();
    else if ((vResultado == null) && (aComponenteCampo instanceof CCoresComboBox))
    //Object o = ((JComboBox)fComponenteCampo).getModel().getSelectedItem();
    vResultado = getValorComponenteStr();
    else if ((vResultado == null) && (fComponenteCampo instanceof JComboBox))
    vResultado = getValorComponenteStr();
    return vResultado;
    }
     */
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    /*
    public String getValorCampoRegistroBdStr() {
    return getValorCampoRegistroBdStr(fComponenteCampo);
    }
     */
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public int setValorOriginalCampoRegistroBd(String aValor) {
        return -1;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public String getDelimitador() {
        switch (fTipoCampo) {
            case cor:
            case inteiro:
            case decimal:
            case logico: {
                return "";
            }
            case texto:
            case memorando: {
                return "'";
            }
        }
        return "";
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public boolean getCampoEditavel() {
        return fEditavel;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void setCores(boolean aEditar, boolean aIgualOriginal) {
        Component vComponente = getComponente();
        if (vComponente != null) {
            if (aEditar) {
                if (aIgualOriginal) {
                    if (fEditavel) {
                        vComponente.setBackground(new Color(255, 255, 255));
                        vComponente.setForeground(new Color(0, 0, 0));
                    } else {
                        vComponente.setBackground(new Color(255, 255, 255));
//                      vComponente.setBackground(new Color(255,255,204));
                        vComponente.setForeground(new Color(0, 0, 0));
                    }
                } else {
                    vComponente.setBackground(new Color(255, 255, 255));
//                  vComponente.setBackground(new Color(255,204,102));
                    vComponente.setForeground(new Color(0, 0, 0));
                }
            } else {
                vComponente.setBackground(new Color(255, 255, 255));
//              vComponente.setBackground(new Color(200,200,200));
                vComponente.setForeground(new Color(0, 0, 128));
            }
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void setEditar(boolean aEditar, boolean aIgualOriginal) {
        if (fComponenteCampo instanceof JTextField) {
            ((JTextField) fComponenteCampo).setEditable(aEditar);
        } else if (fComponenteCampo instanceof JTextArea) {
            ((JTextArea) fComponenteCampo).setEditable(aEditar);
        } else if (fComponenteCampo instanceof CCalendarioComboBox) {
            ((CCalendarioComboBox) fComponenteCampo).setEditable(aEditar);
            ((CCalendarioComboBox) fComponenteCampo).setEnabled(aEditar);
        } else if (fComponenteCampo instanceof CCoresComboBox) {
            ((CCoresComboBox) fComponenteCampo).setEditable(false);
            ((CCoresComboBox) fComponenteCampo).setEnabled(aEditar);
        } else if (fComponenteCampo instanceof JComboBox) {
            ((JComboBox) fComponenteCampo).setEditable(false);
            ((JComboBox) fComponenteCampo).setEnabled(aEditar);
            Component c = ((JComboBox) fComponenteCampo).getEditor().getEditorComponent();
            JTextField tf = (JTextField) c;
            tf.setEnabled(aEditar);
        } else if (fComponenteCampo instanceof JCheckBox) {
            ((JCheckBox) fComponenteCampo).setEnabled(aEditar);
        }
        setCores(aEditar, aIgualOriginal);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public boolean getCampoGradeBd() {
        return fCampoGradeBd;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public ETipoCampoBd getTipoCampo() {
        return fTipoCampo;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public Component getComponente() {
        return fComponenteCampo;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void setFoco() {
        fEventoEstruturaCampoRegistroBd.eventoAntesFocar();
        fComponenteCampo.requestFocus();
        fEventoEstruturaCampoRegistroBd.eventoDepoisFocar();
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public boolean getSelect() {
        return fSelect;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public boolean getInsert() {
        return fInsert;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public boolean getUpdate() {
        return fUpdate;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public boolean getRelatorio() {
        return (fLarguraColunaRelatorio > 0);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void setIndiceTab(int aIndiceTab) {
        fIndiceTab = aIndiceTab;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public int getIndiceTab() {
        return fIndiceTab;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void eventoAntesAtribuicaoValor(CCampoRegistroBd aCRBD) {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void eventoDepoisAtribuicaoValor(CCampoRegistroBd aCRBD) {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void eventoAntesMoverRegistro() {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void eventoDepoisMoverRegistro(CRegistro aRegistro) {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void eventoAntesAtualizarPainelBd(CRegistro aRegistro) {
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
    public void eventoDepoisAtualizarPainelBd(CRegistro aRegistro) {
    }
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/
}
